package com.ruoyi.gb.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gb.domain.SmParticulars;
import com.ruoyi.gb.service.ISmParticularsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目基础信息Controller
 *
 * @author rww
 * @date 2024-05-21
 */
@RestController
@RequestMapping("/gb/particulars")
public class SmParticularsController extends BaseController
{
    @Autowired
    private ISmParticularsService smParticularsService;

    /**
     * 查询项目基础信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SmParticulars smParticulars)
    {
        startPage();
        List<SmParticulars> list = smParticularsService.selectSmParticularsList(smParticulars);
        return getDataTable(list);
    }

    /**
     * 导出项目基础信息列表
     */
    @Log(title = "项目基础信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SmParticulars smParticulars)
    {
        List<SmParticulars> list = smParticularsService.selectSmParticularsList(smParticulars);
        ExcelUtil<SmParticulars> util = new ExcelUtil<SmParticulars>(SmParticulars.class);
        util.exportExcel(response, list, "项目基础信息数据");
    }

    /**
     * 获取项目基础信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(smParticularsService.selectSmParticularsById(id));
    }

    @GetMapping(value = "/selectSmParticularsByPId/{pid}")
    public AjaxResult selectSmParticularsByPId(@PathVariable("pid") String pid){
        return success(smParticularsService.selectSmParticularsByPId(pid));
    }

    /**
     * 新增项目基础信息
     */
    @Log(title = "项目基础信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SmParticulars smParticulars)
    {
        return toAjax(smParticularsService.insertSmParticulars(smParticulars));
    }

    /**
     * 修改项目基础信息
     */
    @Log(title = "项目基础信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SmParticulars smParticulars)
    {
        return toAjax(smParticularsService.updateSmParticulars(smParticulars));
    }

    /**
     * 删除项目基础信息
     */
    @Log(title = "项目基础信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(smParticularsService.deleteSmParticularsByIds(ids));
    }

    @GetMapping("/getAllManagedDept")
    public AjaxResult getAllManagedDept() {
        return AjaxResult.success(smParticularsService.getAllManagedDept());
    }

    //按区域统计各市入库面积
    @GetMapping("/provinceAreaList")
    public AjaxResult provinceAreaList(SmParticulars smParticulars) {
        List<Map<String, Object>> list = smParticularsService.selectCityArea(smParticulars);
        return AjaxResult.success(list);
    }

    /**
     * 接口：查询项目个数、高标建成面积、耕地面积(按市/县汇总)
     * 说明：根据传入的行政编码pprovinceCode 和 行政等级level(1省，2市，3县)
     * @param smParticulars
     * @return
     */
    @GetMapping("/countAreaList")
    public AjaxResult countAreaList(SmParticulars smParticulars) {
        if(smParticulars==null || smParticulars.getLevel()==null){
            return AjaxResult.error("参数不能为空");
        }
        if("1".equals(smParticulars.getLevel())){
            if(smParticulars.getPprovinceCode()==null || "".equals(smParticulars.getPprovinceCode())){
                return AjaxResult.error("省编码不能为空");
            }
        }
        if("2".equals(smParticulars.getLevel())){
            if(smParticulars.getPshiCode()==null || "".equals(smParticulars.getPshiCode())){
                return AjaxResult.error("市编码不能为空");
            }
        }
        if("3".equals(smParticulars.getLevel())){
            if(smParticulars.getPxianCode()==null || "".equals(smParticulars.getPxianCode())){
                return AjaxResult.error("县编码不能为空");
            }
        }
        List<SmParticulars> list = smParticularsService.countAreaList(smParticulars);
        return AjaxResult.success(list);
    }

    /**
     * 接口：按条件查询项目面积和项目个数
     * @param smParticulars
     * @return
     */
    @GetMapping("/countAreaProjectCount")
    public AjaxResult countAreaProjectCount(SmParticulars smParticulars) {
        SmParticulars smParticularsModel = smParticularsService.countAreaProjectCount(smParticulars);
        return AjaxResult.success(smParticularsModel);
    }


}
